﻿package view.components.footer 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	import model.utils.ModuleConfig;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	/**
	 * ...
	 * @author Lobanov
	 */
	public class MusicPlayer extends MovieClip
	{
		
		//Mini component
		public var musicMask:MovieClip;
		public var onStateIco:MovieClip;
		public var offStateIco:MovieClip;
		public var linkArea:MovieClip;
		
		// Music Player variable
		private var _song:Sound;
		private var _channel:SoundChannel;
		private var _sndTrans:SoundTransform;
		private var _position:Number = 0;
		private var _playList:Array;
		private var _selectedIndexPL:Number = 0;
		private var _statePlayer:Boolean = false;
		
		//Click State
		private var stateClick, stateDoubleClick:int;
		//timer
		private var clickTimer:Timer;
		
		public function MusicPlayer($songXml:XMLList) 
		{
			//init view
			onStateIco.alpha = linkArea.alpha = 0;
			
			linkArea.buttonMode = true;
			linkArea.doubleClickEnabled = true;
			linkArea.addEventListener(MouseEvent.DOUBLE_CLICK, doubleHandle);
			linkArea.addEventListener(MouseEvent.CLICK, clickHandle);
			
			linkArea.addEventListener(MouseEvent.ROLL_OVER, overHandle);
			linkArea.addEventListener(MouseEvent.ROLL_OUT, outHandle);
			
			//init play list
			_playList = new Array();
			for each (var song:XML in $songXml.song) {
				_playList.push({url:ModuleConfig.DOMAIN + song.@link, name:song.@name});
			}
			
			//init timer
			clickTimer = new Timer(1);
			
			//init song
			_sndTrans = new SoundTransform(1);
			
			_song = new Sound();
			_song.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
			_song.load(new URLRequest(_playList[_selectedIndexPL].url));
			_position = 0;/**/
			
			/**
			 * EVENTS
			 */
			function overHandle(e:MouseEvent):void {
				TweenLite.to(musicMask, 1.5 , { x:-9, ease:Circ.easeOut } );
			}
			
			function outHandle(e:MouseEvent):void {
				TweenLite.to(musicMask, 1.5 , { x:-64.5, ease:Circ.easeOut} );
			}
		}
		
		//One click (and start timer for checking 2-3 click state)
		private function clickHandle(e:MouseEvent):void {	
			stateClick = 1;	
			stateDoubleClick = 0;
			
			if (!clickTimer.running || clickTimer == null) {
				clickTimer = new Timer(300, 1);
				clickTimer.addEventListener("timer", timerClickHandler);
				clickTimer.start();
			} else {
				//triple click
				stateDoubleClick = 0;
				stateClick = 0;
			}
		}
		
		//Set state of double click 
		private function doubleHandle(e:MouseEvent):void {	
			stateDoubleClick = 1;
			stateClick = 0;
		}
		
		//Check state of double and one click
		public function timerClickHandler(event:TimerEvent):void {
			if (stateDoubleClick == 1) nextTrack();//double click
			if (stateClick == 1) playPouse();// one click
        }
		
		/**
		 * IO ERROR HANDLER MUSIC
		 * @param	event
		 */
		private function ioErrorHandler(event:Event):void {
			trace("ioErrorHandler: " + event);
		}
		
		private function stateButton($b:Boolean):void {
			var alphaChanel:int = ($b)?1:0;
			TweenLite.to(onStateIco, .5 , { alpha:alphaChanel, ease:Circ.easeOut } );
		}
		
		/**********************************************
		 * PLAYER CORE FUNCTION
		 **********************************************/
		public function playPouse():void {
			if (_statePlayer) {
				_position = _channel.position;
				TweenLite.to( _channel, 1 , {volume:0, onComplete:stopSound} );
				_statePlayer = false;
				//trace("Не играй музон");
			} else {
				//trace("Играй музон");
				_channel = _song.play(_position); 
				_sndTrans.volume = 0;
				_channel.soundTransform = _sndTrans;
				TweenLite.to( _channel, 2 , {volume:1} );
				_channel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
				_statePlayer = true;
			}
			
			function stopSound():void {
				//trace("Точно остановили!");
				_statePlayer ? null : _channel.stop();
			}
			
			stateButton(_statePlayer);
		}
		
		public function nextTrack():void {
			_selectedIndexPL = (_selectedIndexPL == _playList.length-1)?  0 : ++_selectedIndexPL;
			
			try {
				_song.close();
			} catch (error) { }
			
			try {
				_channel.stop();
			}catch(error) { }
			
			_song = new Sound();
			_song.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
			_song.load(new URLRequest(_playList[_selectedIndexPL].url));
			_channel = _song.play();
			_channel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
			_statePlayer = true;
			_position = 0;
			
			_sndTrans.volume = 0;
			_channel.soundTransform = _sndTrans;
			TweenLite.to( _channel, 2 , { volume:1 } );
			
			stateButton(_statePlayer);
		}
		
		/*When a song ends, this will take you to the next song.*/
		private function soundComplete(e:Event):void {
			_selectedIndexPL = (_selectedIndexPL == _playList.length-1)?  0 : ++_selectedIndexPL;
			_song = new Sound();
			_song.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
			_song.load(new URLRequest(_playList[_selectedIndexPL].url));
			_channel = _song.play();
			_channel.addEventListener(Event.SOUND_COMPLETE, soundComplete, false, 0, true);
			_statePlayer = true;
			
			stateButton(_statePlayer);
		}
	}
}